<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>环形链表 - 算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .code-dots {
            display: flex;
            gap: 8px;
        }
        
        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            color: #d4d4d4;
            line-height: 1.6;
        }
        
        .keyword { color: #569cd6; }
        .type { color: #4ec9b0; }
        .function { color: #dcdcaa; }
        .comment { color: #6a9955; }
        .string { color: #ce9178; }
        
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-20px); }
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .complexity-badge {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 0.875rem;
            font-weight: 500;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 relative overflow-hidden">
        <div class="absolute top-10 right-10 floating-icon">
            <i class="fas fa-circle-notch text-6xl opacity-20"></i>
        </div>
        <div class="absolute bottom-10 left-10 floating-icon" style="animation-delay: 1.5s;">
            <i class="fas fa-link text-5xl opacity-20"></i>
        </div>
        <div class="container mx-auto px-6 text-center relative z-10">
            <h1 class="text-5xl md:text-6xl font-bold mb-6" style="font-family: 'Noto Serif SC', serif;">
                环形链表
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                探索快慢指针的优雅之道
            </p>
            <div class="flex justify-center gap-4">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-clock mr-2"></i>时间复杂度 O(n)
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-memory mr-2"></i>空间复杂度 O(1)
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12">
        <!-- Problem Description -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <div class="flex items-center mb-6">
                <i class="fas fa-question-circle text-3xl text-purple-600 mr-4"></i>
                <h2 class="text-3xl font-bold">题目描述</h2>
            </div>
            <p class="text-lg leading-relaxed text-gray-700">
                给定一个链表，判断链表中是否有环。如果链表中存在环，则返回 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">true</code>；
                否则，返回 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">false</code>。
                环是指链表的尾部连接到链表中的某个位置（不一定是头部）。
            </p>
        </div>

        <!-- Core Algorithm -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <div class="flex items-center mb-6">
                <i class="fas fa-brain text-3xl text-indigo-600 mr-4"></i>
                <h2 class="text-3xl font-bold">核心算法</h2>
            </div>
            <div class="bg-gradient-to-r from-indigo-50 to-purple-50 p-6 rounded-xl">
                <h3 class="text-2xl font-semibold mb-3 text-indigo-800">
                    <i class="fas fa-rabbit mr-2"></i>快慢指针（Floyd 判圈算法）
                </h3>
                <p class="text-gray-700 leading-relaxed">
                    这是一个经典的算法，也被称为"龟兔赛跑"算法。通过设置两个指针以不同速度遍历链表，
                    如果存在环，它们必然会在环内相遇。
                </p>
            </div>
        </div>

        <!-- Visual Representation -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
            <div class="flex items-center mb-6">
                <i class="fas fa-project-diagram text-3xl text-green-600 mr-4"></i>
                <h2 class="text-3xl font-bold">算法可视化</h2>
            </div>
            <div class="mermaid">
                graph LR
                    A[开始] --> B[初始化快慢指针]
                    B --> C{快指针到达末尾?}
                    C -->|是| D[返回 false]
                    C -->|否| E[慢指针前进一步]
                    E --> F[快指针前进两步]
                    F --> G{快慢指针相遇?}
                    G -->|是| H[返回 true]
                    G -->|否| C
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#27c93f,stroke:#fff,stroke-width:2px,color:#fff
                    style H fill:#27c93f,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Solution Approaches -->
        <div class="grid md:grid-cols-2 gap-8 mb-8">
            <!-- Fast-Slow Pointer -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-center justify-between mb-4">
                    <h3 class="text-2xl font-bold flex items-center">
                        <i class="fas fa-star text-yellow-500 mr-3"></i>
                        快慢指针法
                    </h3>
                    <span class="complexity-badge">最优解</span>
                </div>
                <ul class="space-y-3 text-gray-700">
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <span>快指针每次走两步，慢指针走一步</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <span>若存在环，两指针必相遇</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <span>空间复杂度仅为 O(1)</span>
                    </li>
                </ul>
                <div class="mt-6 p-4 bg-green-50 rounded-lg">
                    <p class="text-sm text-green-800">
                        <i class="fas fa-lightbulb mr-2"></i>
                        <strong>优势：</strong>不需要额外空间，算法优雅高效
                    </p>
                </div>
            </div>

            <!-- Hash Table -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-center justify-between mb-4">
                    <h3 class="text-2xl font-bold flex items-center">
                        <i class="fas fa-hashtag text-blue-500 mr-3"></i>
                        哈希表法
                    </h3>
                    <span class="bg-gray-200 text-gray-700 px-3 py-1 rounded-full text-sm">次优</span>
                </div>
                <ul class="space-y-3 text-gray-700">
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-blue-500 mt-1 mr-3"></i>
                        <span>记录访问过的节点</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-blue-500 mt-1 mr-3"></i>
                        <span>若重复访问则有环</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-times-circle text-red-500 mt-1 mr-3"></i>
                        <span>空间复杂度为 O(n)</span>
                    </li>
                </ul>
                <div class="mt-6 p-4 bg-blue-50 rounded-lg">
                    <p class="text-sm text-blue-800">
                        <i class="fas fa-info-circle mr-2"></i>
                        <strong>适用场景：</strong>需要记录访问路径时使用
                    </p>
                </div>
            </div>
        </div>

        <!-- Code Implementation -->
        <div class="bg-white rounded-2xl shadow-xl overflow-hidden mb-8">
            <div class="p-8">
                <div class="flex items-center mb-6">
                    <i class="fas fa-code text-3xl text-purple-600 mr-4"></i>
                    <h2 class="text-3xl font-bold">示例代码</h2>
                </div>
            </div>
            
            <div class="code-block">
                <div class="code-header">
                    <div class="code-dots">
                        <div class="dot dot-red"></div>
                        <div class="dot dot-yellow"></div>
                        <div class="dot dot-green"></div>
                    </div>
                    <span class="text-gray-400 text-sm">Java</span>
                </div>
                <pre><code><span class="comment">/**
 * 链表节点定义
 */</span>
<span class="keyword">public class</span> <span class="type">ListNode</span> {
    <span class="keyword">int</span> val;
    <span class="type">ListNode</span> next;
    <span class="function">ListNode</span>(<span class="keyword">int</span> x) {
        val = x;
        next = <span class="keyword">null</span>;
    }
}

<span class="keyword">public boolean</span> <span class="function">hasCycle</span>(<span class="type">ListNode</span> head) {
    <span class="comment">// 如果链表为空或只有一个节点，则不可能有环</span>
    <span class="keyword">if</span> (head == <span class="keyword">null</span> || head.next == <span class="keyword">null</span>) {
        <span class="keyword">return false</span>;
    }
    
    <span class="comment">// 初始化快慢指针，都指向头节点</span>
    <span class="type">ListNode</span> slow = head;
    <span class="type">ListNode</span> fast = head;
    
    <span class="comment">// 快指针每次走两步，慢指针每次走一步</span>
    <span class="comment">// 当快指针到达链表末尾时停止</span>
    <span class="keyword">while</span> (fast != <span class="keyword">null</span> && fast.next != <span class="keyword">null</span>) {
        <span class="comment">// 慢指针前进一步</span>
        slow = slow.next;
        <span class="comment">// 快指针前进两步</span>
        fast = fast.next.next;
        
        <span class="comment">// 如果快慢指针相遇，说明链表中存在环